//
//  LRFooterView.m
//  本地生活圈
//
//  Created by admin on 15/8/16.
//  Copyright (c) 2015年 懒人. All rights reserved.
//

#import "LRFooterView.h"

@interface LRFooterView ()

@property (weak, nonatomic) IBOutlet UIButton *sendButton;
@property (weak, nonatomic) IBOutlet UIView *starView;
@property (weak, nonatomic) IBOutlet UIImageView *star1;
@property (weak, nonatomic) IBOutlet UIImageView *star2;
@property (weak, nonatomic) IBOutlet UIImageView *star3;
@property (weak, nonatomic) IBOutlet UIImageView *star4;
@property (weak, nonatomic) IBOutlet UIImageView *star5;
/**
 *  统计分数
 */
@property (nonatomic, assign) int score;
/**
 *  shuju
 */
@property (nonatomic, strong) NSArray *stars;
/**
 *  记录
 */
@property (nonatomic, assign) CGPoint lastPoint;
/**
 *  假为空心 真为实心
 */
@property (nonatomic, assign) BOOL isStop;
@end

@implementation LRFooterView

- (NSArray *)stars
{
    if (!_stars)
    {
        _stars = [NSArray arrayWithObjects:_star1,_star2,_star3,_star4,_star5, nil];
    }
    return _stars;
}

- (IBAction)sendClick {
    // 保存数据到沙盒
    
    [self.stars enumerateObjectsUsingBlock:^(UIImageView *star, NSUInteger idx, BOOL *stop) {
        if (star.tag == 1)
        {
            self.score ++;
        }
    }];
    
    // 分数呢
    if ([self.delegate respondsToSelector:@selector(footerSendWithCont: score:)])
    {
        [self.delegate footerSendWithCont:self.contentTextFile.text score:[NSString stringWithFormat:@"%d",self.score]];
    }
    self.contentTextFile.text = @"";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.starView];
    [self.stars enumerateObjectsUsingBlock:^(UIImageView *star, NSUInteger idx, BOOL *stop) {
        if (CGRectContainsPoint(star.frame, currentPoint))
        {
            
            if (star.tag == 0) // 即空心
            {
                if (idx >= 1) //
                {
                    UIImageView *lastView = self.stars[idx - 1]; // 上一个
                    if (lastView.tag == 1) // 上一个是实心
                    {
                        star.image = [UIImage imageNamed:@"五星1"];
                        star.tag = 1; // 实心星星
                    }
                    else
                    {
                        // 把前面的都置为实心
                        for (int i = 0;i <= idx; i++)
                        {
                            UIImageView *star = self.stars[i];
                            star.image = [UIImage imageNamed:@"五星1"];
                            star.tag = 1;
                        }
                    }
                }
                else // 第一个
                {
                    star.image = [UIImage imageNamed:@"五星1"];
                    star.tag = 1; // 实心星星
                }
                
            }
            else  // 实心 把最后一个置为空心
            {
                for (int i = 5 ; i >idx;i--)
                {
                    if (i == 0) return ;
                    
                    UIImageView *star = self.stars[i-1];
                    star.image = [UIImage imageNamed:@"五星2"];
                    star.tag = 0;
                    
                }
                self.isStop = !self.isStop;
            }
            
        }
        
    }];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.starView];

    [self.stars enumerateObjectsUsingBlock:^(UIImageView *star, NSUInteger idx, BOOL *stop) {
        
        if (CGRectContainsPoint(star.frame, currentPoint))
        {

            if (!self.isStop) // 即空心
            {
                if (idx >= 1) //
                {
                    UIImageView *lastView = self.stars[idx - 1]; // 上一个
                    if (lastView.tag == 1) // 上一个是实心
                    {
                        star.image = [UIImage imageNamed:@"五星1"];
                        star.tag = 1; // 实心星星
                    }
                }
                else // 第一个
                {
                    star.image = [UIImage imageNamed:@"五星1"];
                    self.score ++;
                    star.tag = 1; // 实心星星
                }
                
            }
            else
            {
                star.image = [UIImage imageNamed:@"五星2"];
                star.tag = 0;
            }
            
        }

    }];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.isStop = !self.isStop;
}

/* UITapGes  放弃治疗
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.stars enumerateObjectsUsingBlock:^(UIImageView *star, NSUInteger idx, BOOL *stop) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(starChange:)];
        [star addGestureRecognizer:tap];
        star.userInteractionEnabled = YES;
    }];
}

- (void)starChange:(UIImageView *)star
{
    LRLog(@"%@",star);
    if (star.tag == 0) // 即空心
    {
        star.image = [UIImage imageNamed:@"五星1"];
        self.score ++;
        star.tag = 1; // 实心星星
    }
    else
    {
        star.image = [UIImage imageNamed:@"五星2"];
        self.score --;
        star.tag = 0;
    }
}
*/
@end
